<template>
  <div class="user-profile">
    <van-nav-bar
      title="个人信息"
      left-arrow
      class="page-nav-bar"
      @click-left="$router.back()"
    />
    <input type="file" hidden ref="file" @change="onFileChange" />
    <van-cell title="头像" is-link>
      <van-image class="avatar" fit="cover" round :src="user.photo" @click="$refs.file.click()"/>
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isUpdateNameShow = true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isUpdateGender = true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isUpdateBrithday = true"
    />
    <!-- 编辑昵称 -->
    <van-popup
      v-model="isUpdateNameShow"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <update-name
        @close="isUpdateNameShow = false"
        v-if="isUpdateNameShow"
        v-model="user.name"
      />
    </van-popup>
    <van-popup v-model="isUpdateGender" position="bottom">
      <update-gender
        @close="isUpdateGender = false"
        v-if="isUpdateGender"
        v-model="user.gender"
      />
    </van-popup>
    <van-popup v-model="isUpdateBrithday" position="bottom">
      <update-birthday
        v-model="user.birthday"
        @close="isUpdateBrithday = false"
        v-if="isUpdateBrithday"
      />
    </van-popup>
    <van-popup v-model="isUpdatePhoto" position="bottom" :style="{ height: '100%' }">
      <update-photo
        v-model="user.photo"
        @close="isUpdatePhoto = false"
        v-if="isUpdatePhoto"
        :img='img'
        @update-photo="user.photo = $event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/updateGender.vue'
import UpdateBirthday from './components/updateBirthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserIndex',
  components: { UpdateName, UpdateGender, UpdateBirthday, UpdatePhoto },
  props: {},
  data () {
    return {
      user: {}, // 用户信息
      isUpdateNameShow: false,
      isUpdateGender: false,
      isUpdateBrithday: false,
      isUpdatePhoto: false,
      img: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loader()
  },
  mounted () {},
  methods: {
    async loader () {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
        console.log(this.user)
      } catch (err) {
        console.log(err)
        this.$toast('获取用户数据失败')
      }
    },
    // 更新头像
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文章对象获取到 blob 数据
      this.img = window.URL.createObjectURL(file)
      console.log(this.img)
      this.isUpdatePhoto = true
      this.$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
</style>
